{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import Scalar, draw_graph\n",
    "from linear_model import Linear, mse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"357pt\" height=\"517pt\"\n",
       " viewBox=\"0.00 0.00 356.79 516.80\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 512.797)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-512.797 352.792,-512.797 352.792,4 -4,4\"/>\n",
       "<!-- 140289705437696backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140289705437696backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"229.103\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"229.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x1=1.50</text>\n",
       "</g>\n",
       "<!-- 140289705438224backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140289705438224backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M12,-112.719C12,-112.719 64.2051,-112.719 64.2051,-112.719 70.2051,-112.719 76.2051,-118.719 76.2051,-124.719 76.2051,-124.719 76.2051,-159.641 76.2051,-159.641 76.2051,-165.641 70.2051,-171.641 64.2051,-171.641 64.2051,-171.641 12,-171.641 12,-171.641 6,-171.641 0,-165.641 0,-159.641 0,-159.641 0,-124.719 0,-124.719 0,-118.719 6,-112.719 12,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-152 76.2051,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"0,-132.359 76.2051,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705437744backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140289705437744backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"38.1025\" cy=\"-29.9609\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140289705438224backward&#45;&gt;140289705437744backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140289705438224backward&#45;&gt;140289705437744backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M38.1025,-112.509C38.1025,-92.0979 38.1025,-65.346 38.1025,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140289705437888backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140289705437888backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"92.9897,-0.5 92.9897,-59.4219 175.215,-59.4219 175.215,-0.5 92.9897,-0.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-47.4219\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;10.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"92.9897,-39.7812 175.215,-39.7812 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-27.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"92.9897,-20.1406 175.215,-20.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-8.14062\" font-family=\"Menlo\" font-size=\"10.00\">a</text>\n",
       "</g>\n",
       "<!-- 140289705438224backward&#45;&gt;140289705437888backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140289705438224backward&#45;&gt;140289705437888backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M63.0748,-112.509C75.0748,-98.7314 89.5912,-82.065 102.365,-67.3991\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"105.327,-69.3279 109.255,-59.4883 100.048,-64.7303 105.327,-69.3279\"/>\n",
       "<text text-anchor=\"middle\" x=\"113.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">&#45;9.00</text>\n",
       "</g>\n",
       "<!-- 140289705438272backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140289705438272backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M15,-224.938C15,-224.938 67.2051,-224.938 67.2051,-224.938 73.2051,-224.938 79.2051,-230.938 79.2051,-236.938 79.2051,-236.938 79.2051,-271.859 79.2051,-271.859 79.2051,-277.859 73.2051,-283.859 67.2051,-283.859 67.2051,-283.859 15,-283.859 15,-283.859 9,-283.859 3,-277.859 3,-271.859 3,-271.859 3,-236.938 3,-236.938 3,-230.938 9,-224.938 15,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"3,-264.219 79.2051,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"3,-244.578 79.2051,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705438272backward&#45;&gt;140289705438224backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140289705438272backward&#45;&gt;140289705438224backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M37.4497,-224.63C36.8287,-218.628 36.2809,-212.334 35.959,-206.438 35.5251,-198.491 35.5083,-189.978 35.7072,-181.855\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"39.2055,-181.964 36.0595,-171.847 32.2098,-181.717 39.2055,-181.964\"/>\n",
       "<text text-anchor=\"middle\" x=\"57.1743\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.50</text>\n",
       "</g>\n",
       "<!-- 140289705438032backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140289705438032backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"96,-112.719 96,-171.641 172.205,-171.641 172.205,-112.719 96,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;5.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"96,-152 172.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"96,-132.359 172.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"134.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140289705438272backward&#45;&gt;140289705438032backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140289705438272backward&#45;&gt;140289705438032backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M65.2944,-224.727C76.9194,-210.95 90.9822,-194.284 103.357,-179.618\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"106.258,-181.607 110.032,-171.707 100.908,-177.093 106.258,-181.607\"/>\n",
       "<text text-anchor=\"middle\" x=\"115.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.50</text>\n",
       "</g>\n",
       "<!-- 140289705438320backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140289705438320backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M105,-337.156C105,-337.156 157.205,-337.156 157.205,-337.156 163.205,-337.156 169.205,-343.156 169.205,-349.156 169.205,-349.156 169.205,-384.078 169.205,-384.078 169.205,-390.078 163.205,-396.078 157.205,-396.078 157.205,-396.078 105,-396.078 105,-396.078 99,-396.078 93,-390.078 93,-384.078 93,-384.078 93,-349.156 93,-349.156 93,-343.156 99,-337.156 105,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"93,-376.438 169.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"93,-356.797 169.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705438320backward&#45;&gt;140289705438272backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140289705438320backward&#45;&gt;140289705438272backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M102.231,-336.929C96.6677,-331.03 91.0056,-324.755 85.959,-318.656 79.1297,-310.403 72.2123,-301.168 65.9212,-292.361\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"68.7212,-290.258 60.1048,-284.093 62.9959,-294.285 68.7212,-290.258\"/>\n",
       "<text text-anchor=\"middle\" x=\"107.174\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.50</text>\n",
       "</g>\n",
       "<!-- 140289705437648backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140289705437648backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"132.103\" cy=\"-254.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.50</text>\n",
       "</g>\n",
       "<!-- 140289705438320backward&#45;&gt;140289705437648backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140289705438320backward&#45;&gt;140289705437648backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M131.363,-336.946C131.548,-316.535 131.791,-289.784 131.947,-272.59\"/>\n",
       "</g>\n",
       "<!-- 140289705437840backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140289705437840backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M203,-112.719C203,-112.719 255.205,-112.719 255.205,-112.719 261.205,-112.719 267.205,-118.719 267.205,-124.719 267.205,-124.719 267.205,-159.641 267.205,-159.641 267.205,-165.641 261.205,-171.641 255.205,-171.641 255.205,-171.641 203,-171.641 203,-171.641 197,-171.641 191,-165.641 191,-159.641 191,-159.641 191,-124.719 191,-124.719 191,-118.719 197,-112.719 203,-112.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"229.103\" y=\"-159.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"191,-152 267.205,-152 \"/>\n",
       "<text text-anchor=\"middle\" x=\"229.103\" y=\"-140\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"191,-132.359 267.205,-132.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"229.103\" y=\"-120.359\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705437840backward&#45;&gt;140289705437696backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140289705437840backward&#45;&gt;140289705437696backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M229.103,-112.509C229.103,-92.0979 229.103,-65.346 229.103,-48.1524\"/>\n",
       "</g>\n",
       "<!-- 140289705437840backward&#45;&gt;140289705437888backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140289705437840backward&#45;&gt;140289705437888backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M199.994,-112.634C194.217,-106.671 188.293,-100.335 182.959,-94.2188 175.627,-85.811 168.083,-76.4458 161.181,-67.5536\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"163.913,-65.3644 155.049,-59.5612 158.36,-69.6255 163.913,-65.3644\"/>\n",
       "<text text-anchor=\"middle\" x=\"204.174\" y=\"-83.0187\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.50</text>\n",
       "</g>\n",
       "<!-- 140289705438368backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140289705438368backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M152.99,-449.375C152.99,-449.375 211.215,-449.375 211.215,-449.375 217.215,-449.375 223.215,-455.375 223.215,-461.375 223.215,-461.375 223.215,-496.297 223.215,-496.297 223.215,-502.297 217.215,-508.297 211.215,-508.297 211.215,-508.297 152.99,-508.297 152.99,-508.297 146.99,-508.297 140.99,-502.297 140.99,-496.297 140.99,-496.297 140.99,-461.375 140.99,-461.375 140.99,-455.375 146.99,-449.375 152.99,-449.375\"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-496.297\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"140.99,-488.656 223.215,-488.656 \"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-476.656\" font-family=\"Menlo\" font-size=\"10.00\">value=10.62</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"140.99,-469.016 223.215,-469.016 \"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-457.016\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140289705438368backward&#45;&gt;140289705438320backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140289705438368backward&#45;&gt;140289705438320backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M168.836,-449.165C162.638,-435.77 155.177,-419.645 148.531,-405.282\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"151.679,-403.75 144.303,-396.145 145.326,-406.69 151.679,-403.75\"/>\n",
       "<text text-anchor=\"middle\" x=\"176.96\" y=\"-419.675\" font-family=\"Menlo\" font-size=\"14.00\">4.50</text>\n",
       "</g>\n",
       "<!-- 140289705437936backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140289705437936backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M199,-337.156C199,-337.156 251.205,-337.156 251.205,-337.156 257.205,-337.156 263.205,-343.156 263.205,-349.156 263.205,-349.156 263.205,-384.078 263.205,-384.078 263.205,-390.078 257.205,-396.078 251.205,-396.078 251.205,-396.078 199,-396.078 199,-396.078 193,-396.078 187,-390.078 187,-384.078 187,-384.078 187,-349.156 187,-349.156 187,-343.156 193,-337.156 199,-337.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-384.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"187,-376.438 263.205,-376.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-364.438\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"187,-356.797 263.205,-356.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-344.797\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705438368backward&#45;&gt;140289705437936backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140289705438368backward&#45;&gt;140289705437936backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M193.288,-449.165C198.464,-435.898 204.685,-419.952 210.248,-405.693\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"213.599,-406.733 213.973,-396.145 207.078,-404.189 213.599,-406.733\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.96\" y=\"-419.675\" font-family=\"Menlo\" font-size=\"14.00\">1.00</text>\n",
       "</g>\n",
       "<!-- 140289705438080backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140289705438080backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M197,-224.938C197,-224.938 249.205,-224.938 249.205,-224.938 255.205,-224.938 261.205,-230.938 261.205,-236.938 261.205,-236.938 261.205,-271.859 261.205,-271.859 261.205,-277.859 255.205,-283.859 249.205,-283.859 249.205,-283.859 197,-283.859 197,-283.859 191,-283.859 185,-277.859 185,-271.859 185,-271.859 185,-236.938 185,-236.938 185,-230.938 191,-224.938 197,-224.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.103\" y=\"-271.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"185,-264.219 261.205,-264.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"223.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"185,-244.578 261.205,-244.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"223.103\" y=\"-232.578\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705437936backward&#45;&gt;140289705438080backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140289705437936backward&#45;&gt;140289705438080backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M224.582,-336.946C224.346,-323.934 224.063,-308.345 223.808,-294.299\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"227.301,-293.861 223.62,-283.926 220.302,-293.988 227.301,-293.861\"/>\n",
       "<text text-anchor=\"middle\" x=\"246.174\" y=\"-307.456\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140289705437600backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140289705437600backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"314.103\" cy=\"-254.398\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"314.103\" y=\"-252.219\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140289705437936backward&#45;&gt;140289705437600backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140289705437936backward&#45;&gt;140289705437600backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M254.651,-337.059C260.309,-331.15 266.042,-324.838 271.103,-318.656 283.468,-303.552 295.811,-284.936 304.08,-271.822\"/>\n",
       "</g>\n",
       "<!-- 140289705438080backward&#45;&gt;140289705437840backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140289705438080backward&#45;&gt;140289705437840backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M224.663,-224.727C225.372,-211.716 226.22,-196.127 226.985,-182.08\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"230.501,-181.883 227.55,-171.707 223.511,-181.502 230.501,-181.883\"/>\n",
       "<text text-anchor=\"middle\" x=\"248.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140289705438080backward&#45;&gt;140289705438032backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140289705438080backward&#45;&gt;140289705438032backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M195.867,-224.806C190.467,-218.847 184.933,-212.524 179.959,-206.438 173.129,-198.08 166.115,-188.807 159.691,-179.994\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"162.423,-177.799 153.737,-171.731 156.743,-181.891 162.423,-177.799\"/>\n",
       "<text text-anchor=\"middle\" x=\"201.174\" y=\"-195.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f97be0eedf0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定义训练数据\n",
    "x1 = Scalar(1.5, label='x1', requires_grad=False)\n",
    "y1 = Scalar(1.0, label='y1', requires_grad=False)\n",
    "x2 = Scalar(2.0, label='x2', requires_grad=False)\n",
    "y2 = Scalar(4.5, label='y2', requires_grad=False)\n",
    "# 反向传播\n",
    "model = Linear()\n",
    "loss = mse([model.error(x1, y1), model.error(x2, y2)])\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"358pt\" height=\"478pt\"\n",
       " viewBox=\"0.00 0.00 357.79 477.58\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 473.578)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-473.578 353.792,-473.578 353.792,4 -4,4\"/>\n",
       "<!-- 140289705437696backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140289705437696backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"38.1025\" cy=\"-18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">x1=1.50</text>\n",
       "</g>\n",
       "<!-- 140289705436688backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140289705436688backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M15,-185.719C15,-185.719 67.2051,-185.719 67.2051,-185.719 73.2051,-185.719 79.2051,-191.719 79.2051,-197.719 79.2051,-197.719 79.2051,-232.641 79.2051,-232.641 79.2051,-238.641 73.2051,-244.641 67.2051,-244.641 67.2051,-244.641 15,-244.641 15,-244.641 9,-244.641 3,-238.641 3,-232.641 3,-232.641 3,-197.719 3,-197.719 3,-191.719 9,-185.719 15,-185.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-232.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"3,-225 79.2051,-225 \"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"3,-205.359 79.2051,-205.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"41.1025\" y=\"-193.359\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705437792backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140289705437792backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-73.5 0,-132.422 76.2051,-132.422 76.2051,-73.5 0,-73.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-120.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-112.781 76.2051,-112.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-93.1406 76.2051,-93.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-81.1406\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705436688backward&#45;&gt;140289705437792backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140289705436688backward&#45;&gt;140289705437792backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M40.3222,-185.509C39.8777,-169.18 39.3228,-148.793 38.879,-132.488\"/>\n",
       "</g>\n",
       "<!-- 140289705436448backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140289705436448backward</title>\n",
       "<polygon fill=\"lightgreen\" stroke=\"black\" stroke-width=\"2\" points=\"94,-73.5 94,-132.422 170.205,-132.422 170.205,-73.5 94,-73.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-120.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;5.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-112.781 170.205,-112.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"2\" points=\"94,-93.1406 170.205,-93.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-81.1406\" font-family=\"Menlo\" font-size=\"10.00\">b</text>\n",
       "</g>\n",
       "<!-- 140289705436688backward&#45;&gt;140289705436448backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140289705436688backward&#45;&gt;140289705436448backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M64.7742,-185.509C76.1491,-171.731 89.9095,-155.065 102.018,-140.399\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"104.882,-142.428 108.549,-132.488 99.4837,-137.971 104.882,-142.428\"/>\n",
       "<text text-anchor=\"middle\" x=\"113.174\" y=\"-156.019\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140289705436112backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140289705436112backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"188,-73.5 188,-132.422 264.205,-132.422 264.205,-73.5 188,-73.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-120.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"188,-112.781 264.205,-112.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-100.781\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"188,-93.1406 264.205,-93.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-81.1406\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705437744backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140289705437744backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"226.103\" cy=\"-18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"226.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140289705436112backward&#45;&gt;140289705437744backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140289705436112backward&#45;&gt;140289705437744backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M226.103,-73.4105C226.103,-61.0367 226.103,-46.9573 226.103,-36.221\"/>\n",
       "</g>\n",
       "<!-- 140289705435536backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140289705435536backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"132.103\" cy=\"-18\" rx=\"31.1949\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">a=0.00</text>\n",
       "</g>\n",
       "<!-- 140289705436112backward&#45;&gt;140289705435536backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140289705436112backward&#45;&gt;140289705435536backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M193.753,-73.4105C178.458,-59.9118 160.863,-44.3833 148.396,-33.3796\"/>\n",
       "</g>\n",
       "<!-- 140289705437792backward&#45;&gt;140289705437696backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140289705437792backward&#45;&gt;140289705437696backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M38.1025,-73.4105C38.1025,-61.0367 38.1025,-46.9573 38.1025,-36.221\"/>\n",
       "</g>\n",
       "<!-- 140289705437792backward&#45;&gt;140289705435536backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140289705437792backward&#45;&gt;140289705435536backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M70.452,-73.4105C85.7469,-59.9118 103.342,-44.3833 115.809,-33.3796\"/>\n",
       "</g>\n",
       "<!-- 140289705435872backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140289705435872backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M198,-185.719C198,-185.719 250.205,-185.719 250.205,-185.719 256.205,-185.719 262.205,-191.719 262.205,-197.719 262.205,-197.719 262.205,-232.641 262.205,-232.641 262.205,-238.641 256.205,-244.641 250.205,-244.641 250.205,-244.641 198,-244.641 198,-244.641 192,-244.641 186,-238.641 186,-232.641 186,-232.641 186,-197.719 186,-197.719 186,-191.719 192,-185.719 198,-185.719\"/>\n",
       "<text text-anchor=\"middle\" x=\"224.103\" y=\"-232.641\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"186,-225 262.205,-225 \"/>\n",
       "<text text-anchor=\"middle\" x=\"224.103\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"186,-205.359 262.205,-205.359 \"/>\n",
       "<text text-anchor=\"middle\" x=\"224.103\" y=\"-193.359\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705435872backward&#45;&gt;140289705436112backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140289705435872backward&#45;&gt;140289705436112backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M224.623,-185.509C224.919,-169.18 225.289,-148.793 225.585,-132.488\"/>\n",
       "</g>\n",
       "<!-- 140289705435872backward&#45;&gt;140289705436448backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140289705435872backward&#45;&gt;140289705436448backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M195.57,-185.66C189.922,-179.695 184.142,-173.352 178.959,-167.219 171.94,-158.912 164.757,-149.658 158.191,-140.848\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"160.855,-138.563 152.11,-132.583 155.217,-142.712 160.855,-138.563\"/>\n",
       "<text text-anchor=\"middle\" x=\"200.174\" y=\"-156.019\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.50</text>\n",
       "</g>\n",
       "<!-- 140289705437984backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140289705437984backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M105,-297.938C105,-297.938 157.205,-297.938 157.205,-297.938 163.205,-297.938 169.205,-303.938 169.205,-309.938 169.205,-309.938 169.205,-344.859 169.205,-344.859 169.205,-350.859 163.205,-356.859 157.205,-356.859 157.205,-356.859 105,-356.859 105,-356.859 99,-356.859 93,-350.859 93,-344.859 93,-344.859 93,-309.938 93,-309.938 93,-303.938 99,-297.938 105,-297.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-344.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"93,-337.219 169.205,-337.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"93,-317.578 169.205,-317.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-305.578\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705437984backward&#45;&gt;140289705436688backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140289705437984backward&#45;&gt;140289705436688backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M102.231,-297.71C96.6677,-291.812 91.0056,-285.537 85.959,-279.438 79.1297,-271.184 72.2123,-261.95 65.9212,-253.142\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"68.7212,-251.039 60.1048,-244.874 62.9959,-255.067 68.7212,-251.039\"/>\n",
       "<text text-anchor=\"middle\" x=\"107.174\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;1.00</text>\n",
       "</g>\n",
       "<!-- 140289705437600backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140289705437600backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"132.103\" cy=\"-215.18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"132.103\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140289705437984backward&#45;&gt;140289705437600backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140289705437984backward&#45;&gt;140289705437600backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M131.363,-297.727C131.548,-277.317 131.791,-250.565 131.947,-233.371\"/>\n",
       "</g>\n",
       "<!-- 140289705436496backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140289705436496backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M199,-297.938C199,-297.938 251.205,-297.938 251.205,-297.938 257.205,-297.938 263.205,-303.938 263.205,-309.938 263.205,-309.938 263.205,-344.859 263.205,-344.859 263.205,-350.859 257.205,-356.859 251.205,-356.859 251.205,-356.859 199,-356.859 199,-356.859 193,-356.859 187,-350.859 187,-344.859 187,-344.859 187,-309.938 187,-309.938 187,-303.938 193,-297.938 199,-297.938\"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-344.859\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"187,-337.219 263.205,-337.219 \"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-325.219\" font-family=\"Menlo\" font-size=\"10.00\">value=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"187,-317.578 263.205,-317.578 \"/>\n",
       "<text text-anchor=\"middle\" x=\"225.103\" y=\"-305.578\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705436496backward&#45;&gt;140289705435872backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140289705436496backward&#45;&gt;140289705435872backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M224.842,-297.727C224.724,-284.716 224.583,-269.127 224.455,-255.08\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"227.952,-254.675 224.361,-244.707 220.952,-254.738 227.952,-254.675\"/>\n",
       "<text text-anchor=\"middle\" x=\"246.174\" y=\"-268.238\" font-family=\"Menlo\" font-size=\"14.00\">&#45;4.50</text>\n",
       "</g>\n",
       "<!-- 140289705437648backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140289705437648backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"315.103\" cy=\"-215.18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"315.103\" y=\"-213\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.50</text>\n",
       "</g>\n",
       "<!-- 140289705436496backward&#45;&gt;140289705437648backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140289705436496backward&#45;&gt;140289705437648backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M254.598,-297.797C260.261,-291.892 266.009,-285.592 271.103,-279.438 283.641,-264.288 296.288,-245.679 304.785,-232.579\"/>\n",
       "</g>\n",
       "<!-- 140289705436544backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140289705436544backward</title>\n",
       "<path fill=\"#f0f0f0\" stroke=\"black\" d=\"M152.99,-410.156C152.99,-410.156 211.215,-410.156 211.215,-410.156 217.215,-410.156 223.215,-416.156 223.215,-422.156 223.215,-422.156 223.215,-457.078 223.215,-457.078 223.215,-463.078 217.215,-469.078 211.215,-469.078 211.215,-469.078 152.99,-469.078 152.99,-469.078 146.99,-469.078 140.99,-463.078 140.99,-457.078 140.99,-457.078 140.99,-422.156 140.99,-422.156 140.99,-416.156 146.99,-410.156 152.99,-410.156\"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-457.078\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"140.99,-449.438 223.215,-449.438 \"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-437.438\" font-family=\"Menlo\" font-size=\"10.00\">value=10.62</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"140.99,-429.797 223.215,-429.797 \"/>\n",
       "<text text-anchor=\"middle\" x=\"182.103\" y=\"-417.797\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140289705436544backward&#45;&gt;140289705437984backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140289705436544backward&#45;&gt;140289705437984backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M168.836,-409.946C162.638,-396.552 155.177,-380.426 148.531,-366.063\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"151.679,-364.532 144.303,-356.926 145.326,-367.471 151.679,-364.532\"/>\n",
       "<text text-anchor=\"middle\" x=\"176.96\" y=\"-380.456\" font-family=\"Menlo\" font-size=\"14.00\">1.00</text>\n",
       "</g>\n",
       "<!-- 140289705436544backward&#45;&gt;140289705436496backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140289705436544backward&#45;&gt;140289705436496backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" d=\"M193.288,-409.946C198.464,-396.679 204.685,-380.733 210.248,-366.474\"/>\n",
       "<polygon fill=\"deepskyblue\" stroke=\"deepskyblue\" points=\"213.599,-367.514 213.973,-356.926 207.078,-364.97 213.599,-367.514\"/>\n",
       "<text text-anchor=\"middle\" x=\"223.96\" y=\"-380.456\" font-family=\"Menlo\" font-size=\"14.00\">4.50</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f97be0ee6a0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反向传播\n",
    "model = Linear()\n",
    "# 冻结参数a\n",
    "model.a.requires_grad = False\n",
    "loss = mse([model.error(x1, y1), model.error(x2, y2)])\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"354pt\" height=\"428pt\"\n",
       " viewBox=\"0.00 0.00 353.79 427.69\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 423.688)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-423.688 349.792,-423.688 349.792,4 -4,4\"/>\n",
       "<!-- 140289705437696backward -->\n",
       "<g id=\"node1\" class=\"node\"><title>140289705437696backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"42.1025\" cy=\"-18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"42.1025\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">x1=1.50</text>\n",
       "</g>\n",
       "<!-- 140289705377312backward -->\n",
       "<g id=\"node2\" class=\"node\"><title>140289705377312backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-168.422 0,-227.344 76.2051,-227.344 76.2051,-168.422 0,-168.422\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-215.344\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-207.703 76.2051,-207.703 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-195.703\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"0,-188.062 76.2051,-188.062 \"/>\n",
       "<text text-anchor=\"middle\" x=\"38.1025\" y=\"-176.062\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705437312backward -->\n",
       "<g id=\"node4\" class=\"node\"><title>140289705437312backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"4,-72.5 4,-131.422 80.2051,-131.422 80.2051,-72.5 4,-72.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"42.1025\" y=\"-119.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"4,-111.781 80.2051,-111.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"42.1025\" y=\"-99.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"4,-92.1406 80.2051,-92.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"42.1025\" y=\"-80.1406\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705377312backward&#45;&gt;140289705437312backward -->\n",
       "<g id=\"edge7\" class=\"edge\"><title>140289705377312backward&#45;&gt;140289705437312backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M39.3244,-168.193C39.8207,-156.54 40.3927,-143.11 40.8882,-131.475\"/>\n",
       "</g>\n",
       "<!-- 140289705377504backward -->\n",
       "<g id=\"node7\" class=\"node\"><title>140289705377504backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"131.103\" cy=\"-101.961\" rx=\"31.1949\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-99.7812\" font-family=\"Menlo\" font-size=\"10.00\">b=0.00</text>\n",
       "</g>\n",
       "<!-- 140289705377312backward&#45;&gt;140289705377504backward -->\n",
       "<g id=\"edge2\" class=\"edge\"><title>140289705377312backward&#45;&gt;140289705377504backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M66.5109,-168.193C82.9634,-151.577 102.996,-131.346 116.347,-117.863\"/>\n",
       "</g>\n",
       "<!-- 140289705437744backward -->\n",
       "<g id=\"node3\" class=\"node\"><title>140289705437744backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"219.103\" cy=\"-18\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">x2=2.00</text>\n",
       "</g>\n",
       "<!-- 140289705437312backward&#45;&gt;140289705437696backward -->\n",
       "<g id=\"edge13\" class=\"edge\"><title>140289705437312backward&#45;&gt;140289705437696backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M42.1025,-72.295C42.1025,-60.2927 42.1025,-46.7369 42.1025,-36.3004\"/>\n",
       "</g>\n",
       "<!-- 140289705436064backward -->\n",
       "<g id=\"node10\" class=\"node\"><title>140289705436064backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"131.103\" cy=\"-18\" rx=\"31.1949\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"131.103\" y=\"-15.8203\" font-family=\"Menlo\" font-size=\"10.00\">a=0.00</text>\n",
       "</g>\n",
       "<!-- 140289705437312backward&#45;&gt;140289705436064backward -->\n",
       "<g id=\"edge14\" class=\"edge\"><title>140289705437312backward&#45;&gt;140289705436064backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M73.2304,-72.295C87.266,-59.3695 103.258,-44.6422 114.869,-33.9495\"/>\n",
       "</g>\n",
       "<!-- 140289705377456backward -->\n",
       "<g id=\"node5\" class=\"node\"><title>140289705377456backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"182,-168.422 182,-227.344 258.205,-227.344 258.205,-168.422 182,-168.422\"/>\n",
       "<text text-anchor=\"middle\" x=\"220.103\" y=\"-215.344\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"182,-207.703 258.205,-207.703 \"/>\n",
       "<text text-anchor=\"middle\" x=\"220.103\" y=\"-195.703\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"182,-188.062 258.205,-188.062 \"/>\n",
       "<text text-anchor=\"middle\" x=\"220.103\" y=\"-176.062\" font-family=\"Menlo\" font-size=\"10.00\">+</text>\n",
       "</g>\n",
       "<!-- 140289705377456backward&#45;&gt;140289705377504backward -->\n",
       "<g id=\"edge9\" class=\"edge\"><title>140289705377456backward&#45;&gt;140289705377504backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M192.916,-168.193C177.273,-151.685 158.249,-131.609 145.473,-118.126\"/>\n",
       "</g>\n",
       "<!-- 140289705377072backward -->\n",
       "<g id=\"node9\" class=\"node\"><title>140289705377072backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"181,-72.5 181,-131.422 257.205,-131.422 257.205,-72.5 181,-72.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-119.422\" font-family=\"Menlo\" font-size=\"10.00\">grad=&#45;4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"181,-111.781 257.205,-111.781 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-99.7812\" font-family=\"Menlo\" font-size=\"10.00\">value=0.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"181,-92.1406 257.205,-92.1406 \"/>\n",
       "<text text-anchor=\"middle\" x=\"219.103\" y=\"-80.1406\" font-family=\"Menlo\" font-size=\"10.00\">*</text>\n",
       "</g>\n",
       "<!-- 140289705377456backward&#45;&gt;140289705377072backward -->\n",
       "<g id=\"edge8\" class=\"edge\"><title>140289705377456backward&#45;&gt;140289705377072backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M219.797,-168.193C219.673,-156.54 219.53,-143.11 219.406,-131.475\"/>\n",
       "</g>\n",
       "<!-- 140289705376976backward -->\n",
       "<g id=\"node6\" class=\"node\"><title>140289705376976backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"89,-264.344 89,-323.266 165.205,-323.266 165.205,-264.344 89,-264.344\"/>\n",
       "<text text-anchor=\"middle\" x=\"127.103\" y=\"-311.266\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"89,-303.625 165.205,-303.625 \"/>\n",
       "<text text-anchor=\"middle\" x=\"127.103\" y=\"-291.625\" font-family=\"Menlo\" font-size=\"10.00\">value=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"89,-283.984 165.205,-283.984 \"/>\n",
       "<text text-anchor=\"middle\" x=\"127.103\" y=\"-271.984\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705376976backward&#45;&gt;140289705377312backward -->\n",
       "<g id=\"edge12\" class=\"edge\"><title>140289705376976backward&#45;&gt;140289705377312backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M99.916,-264.115C88.8742,-252.462 76.1474,-239.032 65.1226,-227.397\"/>\n",
       "</g>\n",
       "<!-- 140289705437600backward -->\n",
       "<g id=\"node12\" class=\"node\"><title>140289705437600backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"129.103\" cy=\"-197.883\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"129.103\" y=\"-195.703\" font-family=\"Menlo\" font-size=\"10.00\">y1=1.00</text>\n",
       "</g>\n",
       "<!-- 140289705376976backward&#45;&gt;140289705437600backward -->\n",
       "<g id=\"edge6\" class=\"edge\"><title>140289705376976backward&#45;&gt;140289705437600backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M127.713,-264.115C128.049,-248.362 128.454,-229.36 128.739,-215.939\"/>\n",
       "</g>\n",
       "<!-- 140289705377024backward -->\n",
       "<g id=\"node8\" class=\"node\"><title>140289705377024backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"183,-264.344 183,-323.266 259.205,-323.266 259.205,-264.344 183,-264.344\"/>\n",
       "<text text-anchor=\"middle\" x=\"221.103\" y=\"-311.266\" font-family=\"Menlo\" font-size=\"10.00\">grad=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"183,-303.625 259.205,-303.625 \"/>\n",
       "<text text-anchor=\"middle\" x=\"221.103\" y=\"-291.625\" font-family=\"Menlo\" font-size=\"10.00\">value=4.50</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"183,-283.984 259.205,-283.984 \"/>\n",
       "<text text-anchor=\"middle\" x=\"221.103\" y=\"-271.984\" font-family=\"Menlo\" font-size=\"10.00\">&#45;</text>\n",
       "</g>\n",
       "<!-- 140289705377024backward&#45;&gt;140289705377456backward -->\n",
       "<g id=\"edge1\" class=\"edge\"><title>140289705377024backward&#45;&gt;140289705377456backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M220.797,-264.115C220.673,-252.462 220.53,-239.032 220.406,-227.397\"/>\n",
       "</g>\n",
       "<!-- 140289705437648backward -->\n",
       "<g id=\"node13\" class=\"node\"><title>140289705437648backward</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"311.103\" cy=\"-197.883\" rx=\"34.8795\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"311.103\" y=\"-195.703\" font-family=\"Menlo\" font-size=\"10.00\">y2=4.50</text>\n",
       "</g>\n",
       "<!-- 140289705377024backward&#45;&gt;140289705437648backward -->\n",
       "<g id=\"edge4\" class=\"edge\"><title>140289705377024backward&#45;&gt;140289705437648backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M248.595,-264.115C264.31,-247.715 283.399,-227.793 296.316,-214.313\"/>\n",
       "</g>\n",
       "<!-- 140289705377072backward&#45;&gt;140289705437744backward -->\n",
       "<g id=\"edge3\" class=\"edge\"><title>140289705377072backward&#45;&gt;140289705437744backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M219.103,-72.295C219.103,-60.2927 219.103,-46.7369 219.103,-36.3004\"/>\n",
       "</g>\n",
       "<!-- 140289705377072backward&#45;&gt;140289705436064backward -->\n",
       "<g id=\"edge5\" class=\"edge\"><title>140289705377072backward&#45;&gt;140289705436064backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M188.324,-72.295C174.447,-59.3695 158.634,-44.6422 147.154,-33.9495\"/>\n",
       "</g>\n",
       "<!-- 140289705377648backward -->\n",
       "<g id=\"node11\" class=\"node\"><title>140289705377648backward</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"132.99,-360.266 132.99,-419.188 215.215,-419.188 215.215,-360.266 132.99,-360.266\"/>\n",
       "<text text-anchor=\"middle\" x=\"174.103\" y=\"-407.188\" font-family=\"Menlo\" font-size=\"10.00\">grad=1.00</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"132.99,-399.547 215.215,-399.547 \"/>\n",
       "<text text-anchor=\"middle\" x=\"174.103\" y=\"-387.547\" font-family=\"Menlo\" font-size=\"10.00\">value=10.62</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"132.99,-379.906 215.215,-379.906 \"/>\n",
       "<text text-anchor=\"middle\" x=\"174.103\" y=\"-367.906\" font-family=\"Menlo\" font-size=\"10.00\">mse</text>\n",
       "</g>\n",
       "<!-- 140289705377648backward&#45;&gt;140289705376976backward -->\n",
       "<g id=\"edge10\" class=\"edge\"><title>140289705377648backward&#45;&gt;140289705376976backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M159.746,-360.037C153.915,-348.384 147.194,-334.954 141.372,-323.319\"/>\n",
       "</g>\n",
       "<!-- 140289705377648backward&#45;&gt;140289705377024backward -->\n",
       "<g id=\"edge11\" class=\"edge\"><title>140289705377648backward&#45;&gt;140289705377024backward</title>\n",
       "<path fill=\"none\" stroke=\"deepskyblue\" stroke-dasharray=\"5,2\" d=\"M188.459,-360.037C194.291,-348.384 201.011,-334.954 206.834,-323.319\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f97be10f2b0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 反向传播\n",
    "model = Linear()\n",
    "# 冻结参数a和参数b\n",
    "model.a.requires_grad = False\n",
    "model.b.requires_grad = False\n",
    "loss = mse([model.error(x1, y1), model.error(x2, y2)])\n",
    "loss.backward()\n",
    "draw_graph(loss, 'backward')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
